<?php

class Controller_Admin extends Controller_Abstract
{
	function actionLogin()
	{
		if ($this->_context->isPOST())
		{
			$email = $this->_context->email;
			$password = $this->_context->password;
			$errors = array();
			if (empty($email))
			{
				$errors[] = Util::getMessage(1001);
			}
			if (empty($password))
			{
				$errors[] = Util::getMessage(1002);
			}
			if (count($errors) == 0)
			{
				$adminmodel = new Admin();
				$admin = $adminmodel->valid($email, $password);
				if ($admin == false)
				{
					$errors[] = Util::getMessage(1003);
				}
			}
			if (count($errors) == 0)
			{
				$this->_app->changeCurrentUser($admin, $admin['role']);
				setcookie(Q::ini('acl_cookie_key'), $admin['id'], time()+60*60*24*365*10, '/', $_SERVER['SERVER_NAME'], 0, 1);
				return $this->_redirect(url('default/index'));
			}
			$this->_view['errors'] = $errors;
			$this->_view['email'] = $email;
		}
	}
	function actionLogout()
	{
		setcookie(Q::ini('acl_cookie_key'), 0, time()-3600, '/', $_SERVER['SERVER_NAME'], 0, 1);
		unset($_COOKIE[Q::ini('acl_cookie_key')]);
		$this->_app->cleanCurrentUser();
		session_destroy();
		return $this->_redirect(url('default/index'));
	}
	function actionProfile()
	{
		if ($this->_context->isPOST())
		{
			$errors = array();
			$infos = array();
			$id = $this->_context->id;
			$email = $this->_context->email;
			$name = $this->_context->name;
			$phone = $this->_context->phone;
			if (empty($id))
			{
				$errors[] = Util::getMessage(1000);
			}
			if (empty($email))
			{
				$errors[] = Util::getMessage(1004);
			}
			if (empty($name))
			{
				$errors[] = Util::getMessage(1005);
			}
			if (empty($phone))
			{
				$errors[] = Util::getMessage(1006);
			}
			if (count($errors) == 0)
			{
				$adminmodel = new Admin();
				$adminmodel->update($id, $email, $name, $phone);
				$this->_view['id'] = $id;
				$this->_view['email'] = $email;
				$this->_view['name'] = $name;
				$this->_view['phone'] = $phone;
				if ($id == $_SESSION[Q::ini('acl_session_key')]['id'])
				{
					$_SESSION[Q::ini('acl_session_key')]['email'] = $email;
					$_SESSION[Q::ini('acl_session_key')]['name'] = $name;
					$_SESSION[Q::ini('acl_session_key')]['phone'] = $phone;
				}
				$infos[] = Util::getMessage(1007);
			}
			$this->_view['errors'] = $errors;
			$this->_view['infos'] = $infos;
		}
		else if ($this->_context->isGET())
		{
			if (isset($this->_context->id))
			{
				$id = $this->_context->id;
			}
			else
			{
				$admin = $this->_app->currentUser();
				$id = $admin['id'];
			}
			$adminmodel = new Admin();
			$admin = $adminmodel->getAdminById($id);
			$this->_view['id'] = $admin['id'];
			$this->_view['email'] = $admin['email'];
			$this->_view['name'] = $admin['name'];
			$this->_view['phone'] = $admin['phone'];
		}
	}
	public function actionChangepassword()
	{
		if ($this->_context->isPOST())
		{
			$errors = array();
			$infos = array();
			$admin = $this->_app->currentUser();
			$id = $admin['id'];
			$password = $this->_context->password;
			$password2 = $this->_context->password2;
			if (empty($id))
			{
				$errors[] = Util::getMessage(1000);
			}
			if (empty($password))
			{
				$errors[] = Util::getMessage(1008);
			}
			if (empty($password2))
			{
				$errors[] = Util::getMessage(1009);
			}
			if (count($errors) == 0 && $password != $password2)
			{
				$errors[] = Util::getMessage(1010);
			}
			if (count($errors) == 0)
			{
				$adminmodel = new Admin();
				$adminmodel->resetPassword($id, $password);
				$infos[] = Util::getMessage(1011);
			}
			$this->_view['passworderrors'] = $errors;
			$this->_view['infos'] = $infos;
			$this->_view['email'] = $admin['email'];
			$this->_view['name'] = $admin['name'];
			$this->_view['phone'] = $admin['phone'];
			$this->_viewname = "profile";
		}
	}
	public function actionShow()
	{
		$adminmodel = new Admin();
		$admins = $adminmodel->getAllAdminsExPublishers();
		$this->_view['admins'] = $admins;
		if (isset($_SESSION['infos']))
		{
			$infos = $_SESSION['infos'];
			$this->_view['infos'] = $infos;
			unset($_SESSION['infos']);
		}
	}
	
	public function actionShowpubs(){
		$adminmodel = new Admin();
		$admins = $adminmodel->getAllAdminsPublishers();
		$this->_view['admins'] = $admins;
		if (isset($_SESSION['infos']))
		{
			$infos = $_SESSION['infos'];
			$this->_view['infos'] = $infos;
			unset($_SESSION['infos']);
		}
		$this->_view['pub'] = 1;
		$this->_viewname = 'show';
	}
	
	public function actionAdd()
	{
		if ($this->_context->isGET()){
			if(isset($this->_context->pub))
				$this->_view['pub'] = $this->_context->pub;
			$pubmodel = new Publisher();
			$this->_view['publishers'] = $pubmodel->getAllPublishers();
			
		}elseif ($this->_context->isPOST())
		{
			$errors = array();
			$infos = array();
			$email = $this->_context->email;
			$name = $this->_context->name;
			$phone = $this->_context->phone;
			$password = $this->_context->password;
			$password2 = $this->_context->password2;
			$role = $this->_context->role;
			$company = $this->_context->company;
			$pubId = $this->_context->publisher;
			$subId = $this->_context->sub;
			$adminmodel = new Admin();
			if (empty($email))
			{
				$errors[] = Util::getMessage(1004);
			}else if (!QValidator::validate_is_email($email))
			{
				$errors[] = Util::getMessage(1016);
			}
			if (empty($name))
			{
				$errors[] = Util::getMessage(1005);
			}
			if (empty($phone))
			{
				$errors[] = Util::getMessage(1006);
			}
			if (empty($password))
			{
				$errors[] = Util::getMessage(1008);
			}
			if (empty($password2))
			{
				$errors[] = Util::getMessage(1009);
			}
			if($password != $password2){
				$errors[] = Util::getMessage(1015);
			}
			if (count($errors) == 0)
			{
				if ($password != $password2)
				{
					$errors[] = Util::getMessage(1010);
				}
			}
			if ($adminmodel->checkIfExists($email))
			{
				$errors[] = Util::getMessage(1018);
			}
			if (count($errors) == 0)
			{
				$adminmodel->add($email, $name, $company, $phone, $password, $role, $pubId, $subId);
				if(isset($this->_context->pub)){
					$this->_view['pub'] = $this->_context->pub;
					$pubmodel = new Publisher();
					$this->_view['publishers'] = $pubmodel->getAllPublishers();
					$infos[] = Util::getMessage(3003);
				}else{
					$infos[] = Util::getMessage(1012);
				}
				$this->_view['infos'] = $infos;
			}
			else
			{
				$this->_view['errors'] = $errors;
				$this->_view['email'] = $email;
				$this->_view['name'] = $name;
				$this->_view['phone'] = $phone;
				$this->_view['role'] = $role;
				$this->_view['company'] = $company;
				if(isset($this->_context->pub)){
					$this->_view['pub'] = $this->_context->pub;
					$pubmodel = new Publisher();
					$this->_view['publishers'] = $pubmodel->getAllPublishers();
				}
			}
		}
	}
	function actionEdit()
	{
		if(isset($this->_context->pub)){
				$this->_view['pub'] = $this->_context->pub;
		}
		if ($this->_context->isPOST())
		{
			$errors = array();
			$infos = array();
			$id = $this->_context->id;
			$email = $this->_context->email;
			$name = $this->_context->name;
			$phone = $this->_context->phone;
			$company = $this->_context->company;
			if (empty($id))
			{
				$errors[] = Util::getMessage(1000);
			}
			if (empty($email))
			{
				$errors[] = Util::getMessage(1004);
			}else if (!QValidator::validate_is_email($email))
			{
				$errors[] = Util::getMessage(1016);
			}
			if (empty($name))
			{
				$errors[] = Util::getMessage(1005);
			}
			if (empty($phone))
			{
				$errors[] = Util::getMessage(1006);
			}
			if (count($errors) == 0)
			{
				$adminmodel = new Admin();
				$adminmodel->update($id, $email, $name, $company, $phone);
				$this->_view['id'] = $id;
				$this->_view['email'] = $email;
				$this->_view['name'] = $name;
				$this->_view['phone'] = $phone;
				$this->_view['company'] = $company;
				if ($id == $_SESSION[Q::ini('acl_session_key')]['id'])
				{
					$_SESSION[Q::ini('acl_session_key')]['email'] = $email;
					$_SESSION[Q::ini('acl_session_key')]['name'] = $name;
					$_SESSION[Q::ini('acl_session_key')]['phone'] = $phone;
				}
				$infos[] = Util::getMessage(1007);
			}
			$this->_view['errors'] = $errors;
			$this->_view['infos'] = $infos;
		}
		else if ($this->_context->isGET())
		{
			if (isset($this->_context->id))
			{
				$id = $this->_context->id;
			}
			else
			{
				$admin = $this->_app->currentUser();
				$id = $admin['id'];
			}
			$adminmodel = new Admin();
			$admin = $adminmodel->getAdminById($id);
			$this->_view['id'] = $admin['id'];
			$this->_view['email'] = $admin['email'];
			$this->_view['name'] = $admin['name'];
			$this->_view['phone'] = $admin['phone'];
			$this->_view['company'] = $admin['company'];
			
		}
	}
	public function actionEditpassword()
	{
		if ($this->_context->isPOST())
		{
			$adminmodel = new Admin();
			$errors = array();
			$infos = array();
			$id = $this->_context->id;
			$password = $this->_context->password;
			$password2 = $this->_context->password2;
			if (empty($id))
			{
				$errors[] = Util::getMessage(1000);
			}
			if (empty($password))
			{
				$errors[] = Util::getMessage(1008);
			}
			if (empty($password2))
			{
				$errors[] = Util::getMessage(1009);
			}
			if (count($errors) == 0 && $password != $password2)
			{
				$errors[] = Util::getMessage(1010);
			}
			if (count($errors) == 0)
			{
				$adminmodel->resetPassword($id, $password);
				$infos[] = Util::getMessage(1011);
			}
			$this->_view['passworderrors'] = $errors;
			$this->_view['infos'] = $infos;
			$admin = $adminmodel->getAdminById($id);
			$this->_view['email'] = $admin['email'];
			$this->_view['name'] = $admin['name'];
			$this->_view['phone'] = $admin['phone'];
			$this->_view['id'] = $admin['id'];
			$this->_view['company'] = $admin['company'];
			$this->_viewname = "edit";
		}
	}
	public function actionDelete()
	{
		$id = $this->_context->id;
		if (empty($id))
		{
			return $this->_redirect(url('default/index'));
		}
		$adminmodel = new Admin();
		$adminmodel->deleteAdmin($id);
		$infos = array();
		$infos[] = Util::getMessage(1013);
		$_SESSION['infos'] = $infos;
		if(isset($this->_context->pub))
			return $this->_redirect(url('admin/showpubs'));
		return $this->_redirect(url('admin/show'));
	}
	public function actionDeleteconfirm()
	{
		$id = $this->_context->id;
		if(isset($this->_context->pub))
			$this->_view['pub'] = $this->_context->pub;
		if (empty($id))
		{
			return $this->_redirect(url('default/index'));
		}
		$adminmodel = new Admin();
		$admin = $adminmodel->getAdminById($id);
		if ($admin == false)
		{
			return $this->_redirect(url('default/index'));
		}
		$this->_view['name'] = $admin['name'];
		$this->_view['id'] = $id;
	}
	
	function actionLoadpubs(){
		if($this->_context->isAJAX()){
			$publishermodel = new Publisher();
			$publisers = $publishermodel->getAllPublishers();
			if($publisers){
				echo json_encode($publisers);
			}else{
				echo '';
			}
		}
		exit();
	}
	
	function actionLoadsubs(){
		if($this->_context->isAJAX()){
			$pid = stripslashes($_GET['id']);
			if('' == $pid){
				echo '';
				exit();
			}
			$submodel = new Sub();
			$subs = $submodel->getSubByPubId($pid);
			if($subs){
				echo json_encode($subs);
			}else{
				echo '';
			}
		}
		exit();
	}
	
	function actionSettings(){
		$settingModel = new Settings();
		foreach($_POST as $k=>$v){
			$settingModel->setConf($k, $v);
		}
		$result = $settingModel->getConf();
		foreach($result as $v){
			$conf[$v['k']]=$v['v'];
		}
		$this->_view['conf'] = $conf;
	}




}

?>